package zzs.generalframework.console.core.parameter.commonparameter.address.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.address.AddressBO;
import zzs.generalframework.common.core.parameter.commonparameter.address.AddressInfo;
import zzs.generalframework.common.core.parameter.commonparameter.address.AddressPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.address.dao.IAddressDao;
import zzs.generalframework.console.core.parameter.commonparameter.address.service.IAddressService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.address.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.address.service.impl.addressServiceImpl")
public class AddressServiceImpl implements IAddressService
{

	private Logger logger = LoggerFactory.getLogger(AddressServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IAddressDao addressDao;

	@Override
	public ConsoleReturnBO saveAddress(AddressBO addressBO)
	{
		logger.debug("====保存地址信息开始====");
		logger.debug("====保存地址信息.入参:AddressBO=[{}]====", addressBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AddressPO addressPO = new AddressPO();
		copyAddressBO2AddressPO(addressBO, addressPO);
		Boolean insertResult = addressDao.insert(addressPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存地址信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存地址信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveAddress(List<AddressBO> addressBOList) throws ConsoleException
	{
		logger.info("====保存地址信息列表开始====");
		logger.debug("====保存地址信息列表.入参:AddressBOList=[{}]====", addressBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AddressBO itemBO : addressBOList)
		{
			ConsoleReturnBO itemResult = saveAddress(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存地址信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存地址信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getAddress(AddressInfo addressInfo)
	{
		logger.info("====获取地址信息开始====");
		logger.debug("====获取地址信息.入参:AddressInfo=[{}]====", addressInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AddressPO> addressPOList = addressDao.select(addressInfo);
		if (addressPOList == null || addressPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (addressPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			AddressBO addressBO = new AddressBO();
			copyAddressPO2AddressBO(addressPOList.get(0), addressBO);
			returnConsoleReturnBO.setReturnObj(addressBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			AddressBO addressBO = new AddressBO();
			copyAddressPO2AddressBO(addressPOList.get(0), addressBO);
			returnConsoleReturnBO.setReturnObj(addressBO);
		}
		logger.debug("====获取地址信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取地址信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listAddress(AddressInfo addressInfo)
	{
		logger.info("====获取地址信息列表开始====");
		logger.debug("====获取地址信息列表.入参:AddressInfo=[{}]====", addressInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AddressPO> addressPOList = addressDao.select(addressInfo);
		if (addressPOList == null || addressPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<AddressBO> resultList = new ArrayList<AddressBO>();
			for (AddressPO itemPO : addressPOList)
			{
				AddressBO itemBO = new AddressBO();
				copyAddressPO2AddressBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取地址信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取地址信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAddress(AddressBO addressBO)
	{
		logger.info("====更新地址信息开始====");
		logger.debug("====更新地址信息.入参:AddressBO=[{}]====", addressBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AddressPO addressPO = new AddressPO();
		copyAddressBO2AddressPO(addressBO, addressPO);
		Boolean updateResult = addressDao.update(addressPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新地址信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新地址信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAddress(List<AddressBO> addressBOList) throws ConsoleException
	{
		logger.info("====更新地址信息列表开始====");
		logger.debug("====更新地址信息列表.入参:List<AddressBO>=[{}]====", addressBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AddressBO itemBO : addressBOList)
		{
			ConsoleReturnBO itemResult = updateAddress(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新地址信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新地址信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAddress(AddressBO addressBO)
	{
		logger.info("====删除地址信息开始====");
		logger.debug("====删除地址信息.入参:AddressBO＝[{}]======", addressBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AddressPO addressPO = new AddressPO();
		copyAddressBO2AddressPO(addressBO, addressPO);
		Boolean deleteResult = addressDao.delete(addressPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除地址信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除地址信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAddress(List<AddressBO> addressBOList) throws ConsoleException
	{
		logger.info("====删除地址信息列表开始====");
		logger.debug("====删除地址信息列表.入参:List<AddressBO>＝[{}]======", addressBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AddressBO itemBO : addressBOList)
		{
			ConsoleReturnBO itemResult = removeAddress(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除地址信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除地址信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countAddress(AddressBO addressBO)
	{
		logger.info("====统计地址信息开始====");
		logger.debug("====统计地址信息。入参：AddressBO＝[{}]======", addressBO);
		Long returnLong = new Long(0);
		AddressPO addressPO = new AddressPO();
		copyAddressBO2AddressPO(addressBO, addressPO);
		returnLong = addressDao.count(addressPO);
		logger.debug("====统计地址信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计地址信息结束====");
		return returnLong;
	}

	private void copyAddressBO2AddressPO(AddressBO addressBO, AddressPO addressPO)
	{
		beanUtils.copyBeanNotNull(addressBO, addressPO);
		if (addressBO.getSysID() != null)
		{
			addressPO.setSeqID(addressBO.getSysID());
		}
	}

	private void copyAddressPO2AddressBO(AddressPO addressPO, AddressBO addressBO)
	{
		beanUtils.copyBeanNotNull(addressPO, addressBO);
		if (addressPO.getSeqID() != null)
		{
			addressBO.setSysID(addressPO.getSeqID());
		}
	}

}
